package com.aranandroid.customview.progress;

import static com.blankj.utilcode.util.SizeUtils.dp2px;
import static com.blankj.utilcode.util.SizeUtils.sp2px;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import com.aranandroid.customview.R;


/**
 * 电量控件
 *
 * // xml代码
 *  <com.winspread.wirelessmeasurement.widget.ElectricityView
 *         android:id="@+id/ele"
 *         android:layout_width="60dp"
 *         android:layout_height="22dp">
 *  </com.winspread.wirelessmeasurement.widget.ElectricityView>
 * // 设置电量
 * binding.ele.setEleNumber(78);
 */
public class ElectricityView extends View {

    // 属性介绍
    // left_text_size 左边字体大小
    // left_text_color 左边字体颜色
    // spacing 左右间距
    // ele_color 电量颜色
    // ele_number 电量
    // ele_width 电池宽度
    // ele_height 电池高度
    // ele_inter_width 电量宽度
    // ele_inter_height 电量高度

    // 默认值
    private static float DEFULDE_LEFT_TEXT_SIZE = sp2px(14);
    private static int DEFULDE_LEFT_TEXT_COLOR = Color.parseColor("#1F2336");
    private static float DEFULDE_SPACING = dp2px(6);
    private static int DEFULDE_ELE_COLOR = Color.parseColor("#1F2336");
    private static int DEFULDE_ELE_NUMBER = 100;
    private static float DEFULDE_ELE_WIDTH = dp2px(10);
    private static float DEFULDE_ELE_HEIGHT = dp2px(18);
    private static float DEFULDE_ELE_INTER_WIDTH = dp2px(6);
    private static float DEFULDE_ELE_INTER_HEIGHT = dp2px(13);

    // 可设置属性
    private float leftTextSize;
    private int leftTextColor;
    private float spacing;
    private int eleColor;
    private int eleNumber;
    private float eleWidth;
    private float eleHeight;
    private float eleInterWidth;
    private float eleInterHeight;
    private int layoutwidth;
    private int layoutHeight;

    // 画笔
    // 文字
    Paint paintText = null;
    // 电池头
    Paint paintTop = null;
    // 电池一圈
    Paint paintEle = null;
    // 内部电量
    Paint paintEleInter = null;



    public ElectricityView(Context context) {
        super(context);
    }

    public ElectricityView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ElectricityView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 创建控件的时候获取属性
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ElectricityView);
        leftTextSize = typedArray.getDimension(R.styleable.ElectricityView_left_text_size,DEFULDE_LEFT_TEXT_SIZE);
        leftTextColor = typedArray.getColor(R.styleable.ElectricityView_left_text_color,DEFULDE_LEFT_TEXT_COLOR);
        spacing = (int)typedArray.getDimension(R.styleable.ElectricityView_spacing,DEFULDE_SPACING);
        eleColor = typedArray.getColor(R.styleable.ElectricityView_ele_color,DEFULDE_ELE_COLOR);
        eleNumber = typedArray.getInt(R.styleable.ElectricityView_ele_number,DEFULDE_ELE_NUMBER);
        eleWidth = (int)typedArray.getDimension(R.styleable.ElectricityView_ele_width,DEFULDE_ELE_WIDTH);
        eleHeight = (int)typedArray.getDimension(R.styleable.ElectricityView_ele_height,DEFULDE_ELE_HEIGHT);
        eleInterWidth = (int)typedArray.getDimension(R.styleable.ElectricityView_ele_inter_width,DEFULDE_ELE_INTER_WIDTH);
        eleInterHeight = (int)typedArray.getDimension(R.styleable.ElectricityView_ele_inter_height,DEFULDE_ELE_INTER_HEIGHT);
        layoutwidth = (int)typedArray.getLayoutDimension(R.styleable.ElectricityView_android_layout_width,-1);
        layoutHeight = (int)typedArray.getLayoutDimension(R.styleable.ElectricityView_android_layout_height,-2);
        // 画笔
        // 文字
        paintText = new Paint();
        paintText.setTextSize(leftTextSize);
        paintText.setColor(leftTextColor);
        paintText.setAntiAlias(true);
        // 电池头
        paintTop = new Paint();
        paintTop.setColor(eleColor);
        paintTop.setStyle(Paint.Style.FILL);
        paintTop.setAntiAlias(true);

        // 电池一圈
        paintEle = new Paint();
        paintEle.setColor(eleColor);
        paintEle.setStyle(Paint.Style.STROKE);
        paintEle.setStrokeWidth(dp2px(2));
        paintEle.setAntiAlias(true);
        // 内部电量
        paintEleInter = new Paint();
        paintEleInter.setStyle(Paint.Style.FILL);
        paintEleInter.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String elnum = eleNumber + "%";
        Rect elnumRect = new Rect();
        paintText.getTextBounds(elnum,0,elnum.length(), elnumRect);
        canvas.drawText(elnum,dp2px(2),(elnumRect.height()+getHeight())/2-(elnumRect.height()/15),paintText);

        float eleleft = elnumRect.width() + spacing + dp2px(2);
        float eletop = eleHeight/10+dp2px(2);
        float roundnum = (eleWidth - eleInterWidth)/2;
        float elebottom = eleHeight + dp2px(2);
        canvas.drawRoundRect(eleleft,eletop,eleleft + eleWidth, elebottom, roundnum,roundnum,paintEle);

        float eletopleft = eleleft + eleWidth/3;
        float eletoptop = dp2px(1);
        float eletopright = eleleft + eleWidth/3*2;
        float eletopbottom = eleHeight/10+dp2px(2);
        canvas.drawRoundRect(eletopleft,eletoptop,eletopright, eletopbottom, roundnum/2,roundnum/2,paintTop);

        float eleinterleft = eleleft + roundnum;
        float eleintertop = eletop + roundnum;
        float eleinterright = eleinterleft + eleInterWidth;
        float eleinterbottom = elebottom - roundnum;

        float offset = (eleinterbottom - eleintertop) / 100 * (100-eleNumber);
        eleintertop = offset + eleintertop;

        canvas.drawRoundRect(eleinterleft,eleintertop,eleinterright, eleinterbottom, roundnum/2,roundnum/2,paintEleInter);



    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(layoutwidth, layoutHeight);
    }

    public void setEleNumber(int eleNumber) {
        this.eleNumber = eleNumber;
        invalidate();
    }

    public void setPowerStatus(boolean powerStatus) {
        if(powerStatus){
            paintEleInter.setColor(Color.parseColor("#2CC165"));
        }else {
            paintEleInter.setColor(eleColor);
        }
    }
}
